﻿namespace NJQuery {
	using System;
	using System.Drawing;
	using System.ComponentModel;
	using System.Web.UI.WebControls;

	/// <summary>
	/// Server control used to define the properties of a <see href="http://docs.jquery.com/UI/Effects/Highlight">jQuery UI Highlight effect</see>, which highlights the element's background with a specific color (default is yellow).
	/// </summary>
	public class HighlightEffect : EffectBase {


		#region Constants: Property defaults

		private const string defaultColorString = "#ffff99";
		private const HighlightMode defaultMode = HighlightMode.Show;

		#endregion


		#region Static Readonly Fields: Property defaults

		private static readonly Color defaultColor = Color.FromArgb(255, 255, 153);

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="HighlightEffect"/> class.
		/// </summary>
		public HighlightEffect() : base("highlight") { }

		#endregion


		#region Properties: Initialization options

		/// <summary>
		/// Highlight color.
		/// This property is converted to an HTML color and mapped to the "color" option when the jQuery UI effect is initialized.
		/// </summary>
		[DefaultValue(typeof(Color), defaultColorString), Category("Appearance"), TypeConverter(typeof(WebColorConverter))]
		public virtual Color Color {
			get { return ViewState["Color"] as Color? ?? defaultColor; }
			set { ViewState["Color"] = value; }
		}

		/// <summary>
		/// The mode of the effect.
		/// This property maps directly to the "mode" option when the jQuery UI effect is initialized.
		/// </summary>
		/// <exception cref="ArgumentException">Value not defined.</exception>
		public virtual HighlightMode Mode {
			get { return ViewState["Mode"] as HighlightMode? ?? defaultMode; }
			set {
				if(!Enum.IsDefined(typeof(HighlightMode), value)) throw new ArgumentException("Value not defined.", "value");
				ViewState["Mode"] = value;
			}
		}

		#endregion


		#region Methods: Rendering hooks

		/// <summary>
		/// Allows a jQuery server control to reference scripts and stylesheets necessary to render their client markup and widgets.
		/// </summary>
		/// <param name="manager">The <see cref="Manager"/> used to handle style and script referencing in addition to unified generation of load and ready scripts.</param>
		protected override void Register(Manager manager) {
			base.Register(manager);

			manager.RegisterClientScript(ClientScript.UIHighlightEffect);
		}

		/// <summary>
		/// Returns a <see cref="PropertyCollection"/> containing option values specific to the effect.
		/// This method is used primarily by control developers.
		/// </summary>
		/// <returns>A <see cref="PropertyCollection"/> containing the effect's option values.</returns>
		protected override PropertyCollection CreateOptionProperties() {
			PropertyCollection properties = base.CreateOptionProperties();

			Color color = Color;
			if(color != defaultColor) properties.AddScript("color", color.ToClientSource());

			HighlightMode mode = Mode;
			if(mode != defaultMode) properties.AddEnumValue("mode", mode);

			return properties;
		}

		#endregion


	}

}